package com.mew.chess.core.game.dyj;

import com.mew.chess.core.common.handler.ChessHandlerMapCmdType;
import com.mew.chess.core.common.handler.ChessPlayAnnotation;
import com.mew.chess.core.common.handler.ChessPlayHandler;
import com.mew.chess.core.game.dyj.game.DyjGameManager;
import com.mew.chess.core.game.dyj.game.DyjRoomManager;
import com.mew.chess.core.game.dyj.game.common.DyjLinkType;
import com.mew.chess.core.game.dyj.game.message.PushBetMessage;
import com.mew.chess.core.game.dyj.game.message.UserMessage;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.GameSession;
import com.zebra.remoting.config.EmptyMessage;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * @author dylin
 */
@ChessPlayAnnotation(ChessHandlerMapCmdType.DyjCancle)
@Service
@Slf4j
public class DyjBetCancelHandler implements ChessPlayHandler<EmptyMessage> {
    @Override
    public void handle(ZebraCommand<EmptyMessage> command, GameSession session) {
        long userId = session.getUserId();
        int roomId = session.getRoomId();
        DyjRoomManager roomManager = DyjGameManager.getInstance().getController(roomId);
        if(roomManager.getLinkType()!= DyjLinkType.StartBet.code()){
            log.error("DyjRoomManager stop bet userId:{} roomId: {}", userId, roomId);
            command.sendFailureCode(ResponseCode.StopBet.code());
            return;
        }
        UserMessage user = roomManager.getPlayer(userId);
        for (Map.Entry<Integer, long[]> entry : user.getBetRecord().entrySet()) {
            roomManager.cancelBet(entry.getKey(),entry.getValue()[0]);
        }
        user.updateGoldNumber(user.getAllBet());
        user.clear();
        command.sendSuccessMsg();
    }
}
